package com.chemical.search.view;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
/**
 * @ClassName: BaseController
 * @Description: 基本控制类
 * @author abc
 * @date 2012-1-2 下午01:43:11
 */
public class BaseController {

	/**
	 * 
	 * 函数功能说明:添加绑定参数，尤其是日期绑定
	 * abc  2012-1-2
	 * 修改者名字 修改日期
	 * 修改内容
	 * @参数： @param binder    
	 * @return void   
	 * @throws
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		// 初始化日期
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
		// 初始化基本类型
		binder.registerCustomEditor(Short.class, new CustomNumberEditor(Short.class, true));
		binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, true));
		binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, true));
		binder.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, true));
		binder.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, true));
		binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, true));
		binder.registerCustomEditor(BigInteger.class, new CustomNumberEditor(BigInteger.class, true));
	}

	/**
	 * 获取根目录地址
	 * 
	 * @param request
	 * @return
	 */
	public String getRoot(HttpServletRequest request) {
		return request.getSession().getServletContext().getContextPath();

	}

	/**
	 * 
	 * 函数功能说明:获取session数据
	 * abc  2012-1-2
	 * 修改者名字 修改日期
	 * 修改内容
	 * @参数： @param request
	 * @参数： @param key
	 * @参数： @return    
	 * @return Object   
	 * @throws
	 */
	public Object getSessionAttribute(HttpServletRequest request, String key) {
		return request.getSession().getAttribute(key);
	}

	/**
	 * 
	 * 函数功能说明:设置编码重定向链接
	 * abc  2012-1-2
	 * 修改者名字 修改日期
	 * 修改内容
	 * @参数： @param response
	 * @参数： @param forwardName    
	 * @return void   
	 * @throws
	 */
	public void sendRedirect(HttpServletResponse response, String forwardName) {
		try {
			response.setCharacterEncoding("utf-8");
			response.sendRedirect(forwardName);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
